<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-model-item label="规则编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleCode">
              <a-input v-model="model.ruleCode" placeholder="请输入规则编码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="规则名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleName">
              <a-input v-model="model.ruleName" placeholder="请输入规则名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="最大长度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maxLength">
              <a-input-number v-model="model.maxLength" placeholder="请输入最大长度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="是否补齐" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isPadded">
              <j-dict-select-tag type="radio" v-model="model.isPadded" dictCode="yes_no" placeholder="请选择是否补齐" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-show="model.isPadded=='1'">
            <a-form-model-item label="补齐字符" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paddedChar">
              <a-input v-model="model.paddedChar" placeholder="请输入补齐字符" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12"  v-show="model.isPadded=='1'">
            <a-form-model-item label="补齐方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paddedMethod">
              <j-dict-select-tag type="list" v-model="model.paddedMethod" dictCode="padded_method" placeholder="请选择补齐方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="是否启用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="enableFlag">
              <j-dict-select-tag type="radio" v-model="model.enableFlag" dictCode="yes_no" placeholder="请选择是否启用" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="规则描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleDesc">
              <a-input v-model="model.ruleDesc" placeholder="请输入描述" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" >
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>

  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AutoCodeRuleForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           ruleCode: [
              { required: true, message: '请输入规则编码!'},
           ],
           ruleName: [
              { required: true, message: '请输入规则名称!'},
           ],
           isPadded: [
              { required: true, message: '请输入是否补齐!'},
           ],
           enableFlag: [
              { required: true, message: '请输入是否启用!'},
           ],
        },
        url: {
          add: "/system/autoCodeRule/add",
          edit: "/system/autoCodeRule/edit",
          queryById: "/system/autoCodeRule/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.modelDefault.enableFlag='1';
        this.modelDefault.isPadded='0';
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>